package com.pass.util;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;

import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;

import java.io.File;

import static android.provider.MediaStore.ACTION_IMAGE_CAPTURE;

/**
 * @ClassName CameraUtil
 * @Description 相机工具类
 * @Author guchu
 * @Date 2021/1/3 16:10
 * @Version 1.0
 */
public class CameraUtil {
    /**
     * @param activity
     * @param requestCode 请求码，onActivityResult要用
     *                    获取缩略图
     *                    Bundle bundle = data.getExtras();
     *                    Bitmap bitmap = (Bitmap) bundle.get("data");
     */
    public static void capture(@NonNull Activity activity, int requestCode) {
        Intent intent = new Intent(ACTION_IMAGE_CAPTURE);
        activity.startActivityForResult(intent, requestCode);
    }

    /**
     * 这个调用必须设置FileProvider.请参考 “设置FileProvider.txt”
     *
     * @param activity
     * @param requestCode 请求码，onActivityResult要用
     * @param imgPath     保存图片路径
     *                    onActivityResult() 判断请求成功后，可以直接从imgPath.提取图片.
     *                    int rotatedDegree = BitmapUtil.getRotatedDegree(imgFile.getAbsolutePath());
     *                    Bitmap rawBitmap = BitmapUtil.getBitmapFromSD(imgFile.getAbsolutePath());
     *                    Bitmap bitmap = BitmapUtil.rotateBitmap(rawBitmap, rotatedDegree, false);
     */
    public static void capture(@NonNull Activity activity, int requestCode, @NonNull String authority, String imgPath) {
        File imgFile = new File(imgPath);
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        Uri imgUri = FileProvider.getUriForFile(activity, authority, imgFile);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        activity.startActivityForResult(intent, requestCode);
    }

}
